package com.mayikt.member.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.mayikt.base.BaseResponse;
import com.mayikt.base.BaseWebController;
import com.mayikt.constants.Constants;
import com.mayikt.core.util.VerifyCodeUtil;
import com.mayikt.feign.MemberLoginServiceFeign;
import com.mayikt.member.controller.res.vo.LoginVo;
import com.mayikt.member.input.dto.UserInpDTO;
import com.mayikt.member.input.dto.UserLoginInpDTO;
import com.mayikt.member.service.MemberLoginService;
import com.mayikt.web.util.CookieUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController extends BaseWebController {
    private static final String MB_LOGIN_FTL = "member/login";
    private static final String REDIRECT_INDEX = "redirect:/";
    @Autowired
    private MemberLoginServiceFeign memberLoginServiceFeign;
    /**
     *跳转登陆页面
     * @return
     */
    @GetMapping("/login.html")
    public String getLogin(){
        return MB_LOGIN_FTL;
    }

    @PostMapping("/login.html")
    public String postLogin(@ModelAttribute("loginVo") @Validated LoginVo loginVo,
                            BindingResult bindingResult,
                            Model model,
                            HttpSession httpSession,
                            HttpServletRequest request,
                            HttpServletResponse response){
        if(bindingResult.hasErrors()){
            String errorMsg = bindingResult.getFieldError().getDefaultMessage();
            setErrorMsg(model,errorMsg);
            return MB_LOGIN_FTL;
        }
        //如果传了验证码
        if(!StrUtil.isEmptyIfStr(loginVo.getGraphicCode())){
            Boolean checkBoolean = VerifyCodeUtil.checkVerify(loginVo.getGraphicCode(), httpSession);
            if(!checkBoolean){
                setErrorMsg(model,"验证码错误！");
                return MB_LOGIN_FTL;
            }
        }
        //调用用户服务登陆接口
        UserLoginInpDTO userLoginInpDTO = new UserLoginInpDTO();
        BeanUtils.copyProperties(loginVo,userLoginInpDTO);
        userLoginInpDTO.setLoginType(UserLoginInpDTO.LoginType.PC);
        userLoginInpDTO.setDeviceInfo(webBrowserInfo(request));
        BaseResponse<JSONObject> result = memberLoginServiceFeign.login(userLoginInpDTO);
        if(!Constants.HTTP_RES_CODE_200.equals(result.getCode())){
//            登陆失败
            setErrorMsg(model,result.getMsg());
            return MB_LOGIN_FTL;
        }
//        登陆成功使用token保存用户信息
        String token = result.getData().getString("token");
        CookieUtils.setCookie(request,response, com.mayikt.web.constants.Constants.LOGIN_TOKEN_COOKIENAME,token);
        return REDIRECT_INDEX;
    }
}
